
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()
const _ = db.command
const $ = db.command.aggregate

// 云函数入口函数
exports.main = async (event, context) => {
  console.log(event)
  switch (event.action) {
    case 'publish': return  publish(event)
    case 'list':return list(event)
    case 'del' :return del(event)
    case 'edit' :return edit(event)
    case 'accept' :return accept(event)
    case 'img' :return img(event)
    case 'search':return search(event)
    case 'switchList':return switchList(event)
    case 'editSwitch': return editSwitch(event)
    default: return Promise.reject("unknown action")
  }
}

async function editSwitch(event){
  var id = event._id
  const sw = event.switch

  return new Promise((resolve, reject) => {
    db.collection('contest').doc(id).update({
      data:{
        switch:sw
      }
    }).then(res=>{
      resolve({code:200,message:"修改成功",data:res})
    }).catch(err=>{
      reject({code:300,message:"修改失败",data:err})
    })
  })
}


async function switchList(event){//轮播图队列
  return new Promise((reslove,reject)=>{
    db.collection('contest')
    .aggregate()
    .sort({switch:-1})
    .match({switch:_.gt(0)})
    .end()
  .then(res=>{
    reslove({code:200,message:"获取轮播图成功",data:res})
  }).catch(err=>{
    reject({code:300,message:"获取轮播图失败",data:err})
  })
  })
}

async function publish(event){  //发布比赛信息，传入比赛数据到event.data
  const data = event.data
  var now = new Date()
  return new Promise((reslove,reject)=>{
    db.collection('contest').add({
      data:{
        ...data,
        createTime:now
      }
    })
    .then(res => {
     reslove({code:200,message:"发布成功",data:res})
    })
    .catch(err=>{
      reject({code:300,message:"发布失败",data:err})
    })
  })

}

async function search(event){
  const { pageNumber = 1, pageSize = 10, key = '' } = event.params
  console.log(key)
  console.log(event)
  return new Promise((resolve, reject) => {
    db.collection('contest')
    .where({name: db.RegExp({regexp: key})})
    .skip((pageNumber-1) * pageSize)
    .limit(pageSize)
    .get()
    .then(res=>{
      resolve({code:200,message:"搜索成功",data:res})
    })
    .catch(err=>{
      reject(err)
    })
  });
}

async function list (event){

  var status = event.status == 'all'?_.not(_.eq('del')):event.status
  var type = event.type == 'all'?_.eq('art').or(_.eq('create')).or(_.eq('other')).or(_.eq('sport')):event.type
  return new Promise((reslove,reject)=>{
    db.collection('contest')
    .aggregate()
    .sort({createTime:-1})
    .match({status:status,type:type})
    .end()
    .then(res=>{
      reslove({code:200,message:"获取比赛队列成功",data:res})
    })
    .catch(err=>{
      reject({code:300,message:"获取比赛队列失败",data:err})
    })
  })
 
}

async function del (event){
  var id = event.id
  return new Promise((reslove,reject)=>{

     db.collection('contest').doc(id).update({
      data:{
        status:'del'
      }
    })
    .then(res=>{
      reslove({code:200,message:"删除比赛成功",data:res})
    })
    .catch(err=>{
      reject({code:300,message:"删除比赛失败",data:err})
    })
  })
} 

async function accept(event){
  var status = event.statusd
  var id = evnet.id
  return new Promise((reslove,reject)=>{
     db.collection('contest').doc(id).update({
      data:{
        status
      }
    })
    .then(res=>{
      reslove({code:200,message:"成功",data:res})
    })
    .catch(err=>{
      reject({code:300,message:"失败",data:err})
    })
    
  })
}

async function edit(event){
  var id = event.id
  var params = event.data
  return new Promise((reslove,reject)=>{
     db.collection('contest').doc(id).update({
      data:{
        ...params
      }
    })
    .then(res=>{
      reslove({code:200,message:"修改比赛成功",data:res})
    })
    .catch(err=>{
      reject({code:300,message:"修改失败",data:err})
    })
  })
}

async function img(evevt){
  return new Promise((reslove,reject)=>{
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res){
        reslove(res)
      },
      fail(err){
        reject(err)
      }
    })
  })
}
